import modelEnhance from '@/utils/modelEnhance';
import PageHelper from '@/utils/pageHelper';

/**
 * 当第一次加载完页面时为true
 * 可以用这个值阻止切换页面时
 * 多次初始化数据
 */
let LOADED = false;
export default modelEnhance({
    namespace: 'permission',

    state: {
        pageData: PageHelper.create(),
        groupdict: [],
    },

    subscriptions: {
        setup({dispatch, history}) {
            history.listen(({pathname}) => {
                if (pathname === '/rbac/permission/' && !LOADED) {
                    LOADED = true;
                    dispatch({
                        type: 'init'
                    });
                }
            });
        }
    },

    effects: {
        // 进入页面加载
        * init({payload}, {call, put, select}) {
            const {pageData} = yield select(state => state.permission);
            yield put({
                type: 'getPageInfo',
                payload: {
                    pageData: pageData.startPage(1, 10)
                }
            });
            yield put({
                type: 'getGroupDict'
            });
        },
        //获取菜单字典
        * getGroupDict({playload}, {call, put}){
            console.log('getGroupDict')
            yield put({
                type:'@request',
                afterResponse:resp=>resp.data,
                payload:{
                    valueField:'groupdict',
                    url:'/rbac/permissiongroup/get_dict/',
                    method:'get'
                }
            });
        },
        // 获取分页数据
        * getPageInfo({payload}, {call, put}) {
            const {pageData} = payload;
            yield put({
                type: '@request',
                payload: {
                    valueField: 'pageData',
                    url: '/rbac/permission/',
                    pageInfo: pageData,
                    method: 'get'
                }

            });
        },
        // 保存 之后查询分页
        * save({payload}, {call, put, select, take}) {
            const {values, record, success} = payload;
            const {pageData} = yield select(state => state.permission);
            yield put.resolve({
                type: '@request',
                payload: {
                    notice: true,
                    url: record ? `/rbac/permission/${record.id}/` : '/rbac/permission/',   //新增或者修改
                    method: record ? 'patch' : 'post',
                    data: values,
                    afterResponse: (resp) => {   //D:\xueke\ui\src\config.js ；afterResponse  弹出框退出
                        success();
                    },
                }
            });

            yield put({
                type: 'getPageInfo',
                payload: {pageData}
            });
        },

        // 删除 之后查询分页
        * remove({payload}, {call, put, select}) {
            const {records, success} = payload;
            const {pageData} = yield select(state => state.permission);
            yield put.resolve({
                type: '@request',
                payload: {
                    notice: true,
                    url: `/rbac/permission/${records[0].id}/`,
                    //data: records.map(item => item.rowKey),
                    method: 'delete'
                }
            });
            success();

            yield put({
                type: 'getPageInfo',
                payload: {pageData}
            });
        },
    },
});
